﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SFP.Input
{
    public class KeyboardState
    {
        private Form form;
        private Dictionary<Keys, Keyboard.KeyState> keyStates;

        internal KeyboardState(Dictionary<Keys, Keyboard.KeyState> keyStates, Form form)
        {
            this.keyStates = keyStates;
            this.form = form;
        }

        public bool isKeyUp(Keys key)
        {
            if (!isFormActive()) return true;

            Keyboard.KeyState keyState;
            keyStates.TryGetValue(key, out keyState);
            if (keyState == null) keyState = new Keyboard.KeyState(false, false);
            return !keyState.down;
        }

        public bool isKeyDown(Keys key)
        {
            if (!isFormActive()) return false;

            Keyboard.KeyState keyState;
            keyStates.TryGetValue(key, out keyState);
            if (keyState == null) keyState = new Keyboard.KeyState(false, false);
            return keyState.down;
        }

        public bool isKeyDownNotPressed(Keys key)
        {
            if (!isFormActive()) return false;

            Keyboard.KeyState keyState;
            keyStates.TryGetValue(key, out keyState);
            return keyState.down && !keyState.pressed;
        }

        private bool isFormActive()
        {
            return form == Form.ActiveForm;
        }
    }
}
